		

		<section>

			<ul class="breadcrumb">
        <li><a href="<?php echo base_url('index.php/mainController/'); ?>">Home</a> <span class="divider">/</span></li>
        <li><a href="<?php echo base_url('index.php/employeeController/'); ?>">รายชื่อผู้ใช้งานในระบบ</a> <span class="divider">/</span></li>
        <li class="active">แก้ไขผู้ใช้งานในระบบ</li>
      </ul>

			<div class="page-header">
				<h1>แก้ไขผู้ใช้งานในระบบ</h1>
			</div>

			<script type="text/javascript">

			////////////////////////////////////////////////

      function validateFirstname(firstname){
        var reg = /^[ก-๐a-zA-Z]+$/;
        return firstname.match(reg);
      }

      function validateLastname(lastname){
        var reg = /^[ก-๐a-zA-Z]+$/;
        return lastname.match(reg);
      }

      function validateGeneral(general){
        var reg = /^[ก-๐a-zA-Z0-9\s]+$/;
        return general.match(reg);
      }

      function validateTel(tel){
        var reg = /^0[2|8|9][0-9]{7,8}$/;
        return tel.match(reg);
      }
      ////////////////////////////////////////////////

			$(function(){
				

        var firstnameIsCorrect = true;
        var lastnameIsCorrect = true;
        var telIsCorrect = true;

				$('#btnEdit').bind('click',function(){

					var haveBlank = false;
          $('form').find('input').each(function(){
            //Check Null
            if($(this).val() == "" && $(this).hasClass('require') ){
              $(this).parent().parent().removeAttr('class');
              $(this).parent().parent().addClass('control-group warning');
              var nameInput = $(this).parent().prev().html();
              //alert(nameInput);
              $(this).next().html('กรุณาใส่ '+nameInput);
              haveBlank = true;
            }else{
              $(this).parent().parent().removeClass('warning');
              $(this).next().html('');
            }

          });
          //if see blank stop working loop
          if(haveBlank){
            return;
          }

          if (firstnameIsCorrect == false || lastnameIsCorrect == false || telIsCorrect == false){
            return;
          }

          //alert('aa');

          $.ajax({
            type : 'POST',
            url : '<?php echo base_url("index.php/employeeController/updateEmployee"); ?>',
            data : { 
            	id : "<?php echo $dataUser['id'] ?>", title : $('#title').val(),firstname : $('#firstname').val(),
            	lastname : $('#lastname').val(), position : $('#position').val(),department : $('#department').val(),
            	subdepartment : $('#subdepartment').val(), tel : $('#tel').val(),role : $('#role').val(),
            	status : $('#status').val() ,amcam : $('#amcam').val() ,amcats : $('#amcats').val() },
            success:function(result){
            	//alert(result);
              if(result.indexOf('success') > -1){
                window.location.href = "<?php echo site_url('employeeController'); ?>";
              }else{
              	alert('ข้อมูลผิดพลาด');
              }
            },
            error:function(err){
              alert('Error : '+err);
            }
          });

				});

				$('#firstname').bind('keyup',function(){
          if(validateFirstname($('#firstname').val()) == null){
            
            $('#firstname').parent().parent().removeAttr('class');
            $('#firstname').parent().parent().addClass('control-group error');
            $('#firstname').next().html('ชื่อไม่ถูกต้อง');
            firstnameIsCorrect = false;
            
          }else{
            $('#firstname').parent().parent().removeAttr('class');
            $('#firstname').parent().parent().addClass('control-group success');
            $('#firstname').next().html('');
            firstnameIsCorrect = true;
          }
        });

        $('#lastname').bind('keyup',function(){
          if(validateLastname($('#lastname').val()) == null){
            
            $('#lastname').parent().parent().removeAttr('class');
            $('#lastname').parent().parent().addClass('control-group error');
            $('#lastname').next().html('นามสกุลไม่ถูกต้อง');
            formNotTrue = false;
          }else{
            $('#lastname').parent().parent().removeAttr('class');
            $('#lastname').parent().parent().addClass('control-group success');
            $('#lastname').next().html('');
            formNotTrue = true;
          }
        });

        $('#tel').bind('keyup',function(){

          if ($('#tel').val() == "" ) {
            $('#tel').parent().parent().removeAttr('class');
            $('#tel').parent().parent().addClass('control-group');
            telIsCorrect = true;
            return;
          }

          if(validateTel($('#tel').val()) == null){
            $('#tel').parent().parent().removeAttr('class');
            $('#tel').parent().parent().addClass('control-group error');
            $('#tel').next().html('เบอร์โทรศัพท์ไม่ถูกต้อง');
            telIsCorrect = false;
          }else{
            $('#tel').parent().parent().removeAttr('class');
            $('#tel').parent().parent().addClass('control-group success');
            $('#tel').next().html('');
            telIsCorrect = true;
          }
        });

			});
			</script>

			<div>
				<form class="form-horizontal">
					<div class="control-group">
						<label class="control-label" for="title">คำนำหน้าชื่อ</label>
						<div class="controls">
							<select id="title">
								<?php  
								if(count($resultTitle) > 0){
									foreach ($resultTitle as $row) {

										if($row['ref_code'] == $dataUser['emp_title']){
											$select = "selected='selected'";
										}else{
											$select = "";
										}

										echo "<option value='".$row['ref_code']."' ".$select." >";
										echo iconv('TIS-620', 'UTF-8', $row['ref_name']); 
										echo "</option>";
									}
								}else{
									echo "<option>ไม่มีข้อมูล</option>";
								}
								?>
							</select>
							<span class="help-inline"></span>
						</div>

					</div>



					<div class="control-group">
						<label class="control-label" for="firstname">ชื่อ *</label>
						<div class="controls">
							<input type="text" id="firstname" class="require" placeholder="ชื่อ" value="<?php if(isset($dataUser)) { echo iconv('TIS-620', 'UTF-8', $dataUser['emp_name']) ; } ?>">
							<span class="help-inline"></span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="lastname">นามสกุล *</label>
						<div class="controls">
							<input type="text" id="lastname" class="require" placeholder="นามสกุล" value="<?php if(isset($dataUser)) { echo iconv('TIS-620', 'UTF-8', $dataUser['emp_lastname']) ; } ?>">
							<span class="help-inline"></span>
						</div>
					</div>

					<div class="control-group">
						<label class="control-label" for="position">ตำแหน่ง</label>
						<div class="controls">
							<select id="position">
								<?php  
								if(count($resultPosition) > 0){
									foreach ($resultPosition as $row) {
										
										if($row['ref_code'] == $dataUser['emp_position_code']){
											$select = "selected='selected'";
										}else{
											$select = "";
										}

										echo "<option value='".$row['ref_code']."' ".$select." >";
										echo iconv('TIS-620', 'UTF-8', $row['ref_name']); 
										echo "</option>";
									}
								}else{
									echo "<option>ไม่มีข้อมูล</option>";
								}
								?>
							</select>
							<span class="help-inline"></span>
						</div>
					</div>

					<div class="control-group">
						<label class="control-label" for="department">กอง</label>
						<div class="controls">
							<select id="department">
							<?php  
								if(count($resultDepartment) > 0){
									foreach ($resultDepartment as $row) {

										if($row['ref_code'] == $dataUser['emp_department_code']){
											$select = "selected='selected'";
										}else{
											$select = "";
										}

										echo "<option value='".$row['ref_code']."' ".$select.">";
										echo iconv('TIS-620', 'UTF-8', $row['ref_name']); 
										echo "</option>";
									}
								}else{
									echo "<option>ไม่มีข้อมูล</option>";
								}
							?>
							</select>
							<span class="help-inline"></span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="subdepartment">กองย่อย</label>
						<div class="controls">
							<select id="subdepartment">
							<?php  
								if(count($resultSubdepartment) > 0){
									foreach ($resultSubdepartment as $row) {
										if($row['ref_code'] == $dataUser['emp_subdepartment_code']){
											$select = "selected='selected'";
										}else{
											$select = "";
										}

										echo "<option value='".$row['ref_code']."' ".$select.">";
										echo iconv('TIS-620', 'UTF-8', $row['ref_name']); 
										echo "</option>";
									}
								}else{
									echo "<option>ไม่มีข้อมูล</option>";
								}
							?>
							</select>
							<span class="help-inline"></span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="tel">เบอร์โทรศัพท์</label>
						<div class="controls">
							<input type="text" id="tel" placeholder="เบอร์โทรศัพท์" maxlength="10" value="<?php if(isset($dataUser)) { echo $dataUser['emp_tel']; } ?>">
							<span class="help-inline"></span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="role">Role</label>
						<div class="controls">
							<select id="role">
							<?php  
								if(count($resultRole) > 0){
									foreach ($resultRole as $row) {
										if($row['role_id'] == $dataUser['emp_role']){
                  		$select = "selected='selected'";
                  	}else{
                  		$select = "";
                  	}

										echo "<option value='".$row['role_id']."' ".$select.">";
										echo iconv('TIS-620', 'UTF-8', $row['role_name']); 
										echo "</option>";
									}
								}else{
									echo "<option>ไม่มีข้อมูล</option>";
								}
							?>
							</select>
							<span class="help-inline"></span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="status">สถานะ</label>
						<div class="controls">
							<select id='status'>
								<option value="A" <?php if($dataUser['emp_status'] == 'A') { echo "selected='selected'"; } ?>>Active</option>
								<option value="N" <?php if($dataUser['emp_status'] == 'N') { echo "selected='selected'"; } ?>>Inactive</option>
							</select>
							<span class="help-inline"></span>
						</div>
					</div>

					<div class="control-group">
						<label class="control-label" for="amcam">ชื่อในระบบ Amcam</label>
						<div class="controls">
							<input type="text" id="amcam" placeholder="ชื่อในระบบ Amcam" value="<?php if(isset($dataUser)) { echo $dataUser['amcam_user']; } ?>">
							<span class="help-inline"></span>
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="amcats">ชื่อในระบบ Amcats</label>
						<div class="controls">
							<input type="text" id="amcats" placeholder="ชื่อในระบบ Amcats" value="<?php if(isset($dataUser)) { echo $dataUser['amcats_user']; } ?>">
							<span class="help-inline"></span>
						</div>
					</div>


					<div class="control-group">
						<div class="controls">
							<input id="btnEdit" name="btnEdit" type="button" class="btn btn-primary" value="แก้ไข">
						</div>
					</div>
				</form>
			</div>

		</section>
